<!-- 药品-待出库 -->
<template>
  <div>
    <div ref="search" class="app-search">
      <el-row class="mt10" type="flex">
        <Search :data="selections" :attribute="'createdTime'" @dateChange="handleDateChange" @change="handleTypeChange" />
        <el-form ref="searchFormRef" :model="searchForm" :inline="true">
        <!-- <el-form-item label="选择顾客：" class="marginLeft">
          <el-select v-model="searchForm.value" placeholder="请选择">
            <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item> -->
        </el-form>
      </el-row>
    </div>
    <!-- 列表区域  -->
    <div class="box-show marginTop">
      <el-table v-loading="loading" :data="outboundList" border :row-class-name="tableColor" :height="innerHeight">
        <el-table-column label="领药单号" prop="receiveCode" show-overflow-tooltip />
        <el-table-column label="申请/开单人" prop="receiveUsr" show-overflow-tooltip />
        <el-table-column label="顾客" prop="customerName" show-overflow-tooltip />
        <el-table-column label="科室/部门" prop="deptName" show-overflow-tooltip />
        <el-table-column label="申请/开单时间" prop="createdTime" show-overflow-tooltip>
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createdTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="120">
          <template slot-scope="scope">
            <el-button type="text" @click="outboundBtn(scope.row)">出库</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination :page-num="params.page" :page-size="params.pageSize" :on-change="handlePaginationChange" :total="total" />
    </div>
    <!-- 出库 -->
    <Dialog :title="title" :visible.sync="visible" :button-text="testBtn" :loading="loading" @confirm="submitForm" @close="handleClose">
      <!-- <div class="headStyle">
        <span>领药单号：{{ outboundObj.receiveCode }}</span><span>申请/开单人：{{ outboundObj.receiveUsr }}</span><span>科室部门：{{ outboundObj.deptName }}</span><span>顾客：{{ outboundObj.customerName }}</span>
      </div> -->
      <el-row>
        <el-col :span="6">领药单号：{{ outboundObj.receiveCode }}</el-col>
        <el-col :span="6">申请/开单人：{{ outboundObj.receiveUsr }}</el-col>
        <el-col :span="6">科室部门：{{ outboundObj.deptName }}</el-col>
        <el-col :span="6">顾客：{{ outboundObj.customerName }}</el-col>
      </el-row>
      <el-table v-loading="loading" :data="outboundTable" border :row-class-name="tableColor">
        <el-table-column label="名称" prop="name" :show-overflow-tooltip="true" />
        <el-table-column label="规格" prop="specl" :show-overflow-tooltip="true" />
        <el-table-column label="单位" prop="unit" :show-overflow-tooltip="true" />
        <el-table-column label="开单数量" prop="dataCount" align="center" :show-overflow-tooltip="true" />
        <el-table-column label="出库数量" prop="receiveCount" align="center" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            {{ scope.row.receiveCount }}
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="120">
          <template slot-scope="scope">
            <el-button type="text" @click="batchOutboundBtn(scope.row)">批次出库</el-button>
          </template>
        </el-table-column>
      </el-table>
    </Dialog>
    <!-- 批量出库 -->
    <Dialog :title="titleTwo" :visible.sync="visibleTwo" :button-text="testBtnTwo" :loading="loading" @confirm="submitFormTwo">
      <!-- <div class="headStyle">
        <span>名称：{{ batchOutbound.name }}</span><span>规格：{{ batchOutbound.specl }}</span><span>单位：{{ batchOutbound.unit }}</span><span>开单数量：{{ batchOutbound.dataCount }}</span>
      </div> -->
      <el-row>
        <el-col :span="6">名称：{{ batchOutbound.name }}</el-col>
        <el-col :span="6">规格：{{ batchOutbound.specl }}</el-col>
        <el-col :span="6">单位：{{ batchOutbound.unit }}</el-col>
        <el-col :span="6">开单数量：{{ batchOutbound.dataCount }}</el-col>
      </el-row>
      <el-table v-loading="loading" :data="batchOutboundTable" border :row-class-name="tableColor">
        <el-table-column label="批号" prop="inventoryBatch" :show-overflow-tooltip="true" />
        <el-table-column label="库存" prop="stockCount" :show-overflow-tooltip="true" />
        <el-table-column label="出库数量" align="center" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-input-number v-model="scope.row.receiveCount" controls-position="right" :min="0" :max="scope.row.stockCount-0" @change="handleChange(batchOutboundTable)" />
          </template>
        </el-table-column>
      </el-table>
    </Dialog>
  </div>
</template>
<script>
import Search from "@/components/Search";
import { selections } from "@/config";
import { tableColor } from '@/utils/common.js';
import Dialog from "@/components/Dialog/index";
window.onbeforeunload = event => {
  window.scrollTo(0, 0); // 每次刷新，滚动条在页面最上面
};
export default {
  components: {
    Search,
    Dialog,
  },
  props: {
    activates: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      selections,
      tableColor,
      innerHeight: null,
      searchForm: {},
      options: [],
      params: {
        page: 1,
        pageSize: this.$store.state.commonsettings.maxPageSize,
      },
      total: 0,
      outboundList: [],
      requestInfo: {
        drugType: '领药',
        receiveStatus: '待出库',
      },
      outboundObj: {},
      outboundTable: [],
      batchOutbound: {},
      batchOutboundTable: [],
      title: "",
      testBtn: "", // 对话框按钮名称
      visible: false, // 出库
      titleTwo: "",
      testBtnTwo: "",
      visibleTwo: false, // 批次出库
      loading: false,
      flag: false,
      num: 0, // 批量出库数量
      handleNumArr: [],
    };
  },
  mounted() {
    this.getOutboundList();
    var topHeight = (this.$refs.search.clientHeight + 160 + 125);
    var winHeight = document.body.clientHeight || document.documentElement.clientHeight;
    this.innerHeight = winHeight - topHeight;
  },
  methods: {
    // 待出库列表
    async getOutboundList() {
      this.loading = true;
      const res = await this.$api['warehouse/dispensing/outboundlist']({ ...this.params, query: this.requestInfo });
      this.loading = false;
      if (res.code !== 200) return this.$message.error('查询待出库列表失败！');
      this.outboundList = res.data.result;
      this.total = res.data.total;
    },
    handlePaginationChange(val) {
      this.params = { page: val.pageNum, pageSize: val.pageSize };
      this.getOutboundList();
    },
    // 出库
    async outboundBtn(row) {
      this.outboundObj = row;
      this.title = "药品出库";
      this.testBtn = "提 交";
      this.visible = true;
      const res = await this.$api['warehouse/dispensing/druglist']({
        ...this.params, query: { receiveId: row.id },
      });
      if (res.code === 200) {
        this.outboundTable = res.data.result;
      }
    },
    // 批量出库
    async batchOutboundBtn(row) {
      this.batchOutbound = row;
      this.titleTwo = "药品批次出库";
      this.testBtnTwo = "确 认";
      this.visibleTwo = true;
      const res = await this.$api['warehouse/dispensing/batchDruglist']({
        query: { materialMedicineId: row.drugId },
      });
      if (res.code === 200) {
        this.batchOutboundTable = res.data.result;
      }
    },
    handleChange(val) {
      this.handleNumArr = val;
      this.num = 0;
      for (let i = 0; i < val.length; i++) {
        this.num += val[i].receiveCount;
      }
    },
    // 提交
    async submitForm() {
      this.outboundTable.map(item => {
        if (item.dataCount - 0 === item.receiveCount) {
          this.flag = true;
        } else {
          this.$message.info('批次出库数量合计与开单数量不符，请检查后重试！');
        }
      });
      if (this.flag) {
        const res = await this.$api['warehouse/dispensing/adddeliver']({ listDto: this.outboundTable, drugType: '领药', processor: this.$store.getters.currentUserInfo.name });
        console.log(res);
        if (res.code !== 200) return this.$message.error(res.msg);
        this.$message.success('出库成功！');
        this.getOutboundList();
        this.visible = false;
      }
    },
    // 确认批次
    submitFormTwo() {
      this.visibleTwo = false;
      this.outboundTable.map(item => {
        if (this.batchOutbound.id === item.id) {
          item.receiveCount = this.num;
          item.list = this.handleNumArr;
        }
      });
    },
    handleClose() {
      this.visible = false;
    },
    // 顶部时间筛选
    handleDateChange(val) {
      this.requestInfo.timeType = val.timeType;
      this.requestInfo.createdTime = val.createdTime;
      this.getOutboundList();
    },
    handleTypeChange(val) {
      this.requestInfo.timeType = val.timeType;
      this.requestInfo.createdTime = val.createdTime;
      this.getOutboundList();
    },
  },
};
</script>
<style lang="scss" scoped>
</style>
